package dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import util.DBController;

import model.Booking;
import model.RoomHasBooking;
import dao.intf.RoomHasBookingDao;

public class RoomHasBookingDaoImpl implements RoomHasBookingDao {

	public List<RoomHasBooking> getBookingListByRoomId(String id) {
		List<RoomHasBooking> bookingList = new ArrayList<RoomHasBooking>();
		
		ResultSet rs = DBController.retrieve("Room_Has_Booking", null, "RoomID='"+id+"'");
		
		try {
			if( rs != null ) {
				while( rs.next() ) {
					RoomHasBooking roomHasBooking = new RoomHasBooking();
					roomHasBooking.setRoomId(rs.getString("RoomID"));
					roomHasBooking.setBookingId(rs.getString("BookingID"));
					
					bookingList.add(roomHasBooking);
				}
			}
			DBController.disconnect();
		} catch (Exception e) {
			System.out.println("Error at method getBookingListByRoomId() of RoomHasBookingDaoImpl.");
			e.printStackTrace();
		}
		
		return bookingList;
	}
	public int saveRoomHasBooking(RoomHasBooking roomhasBooking) {
		int recordCount=0;
		try {
			if(roomhasBooking!=null) {
				String queryStr	 = " INSERT INTO Room_Has_Booking ";
						queryStr+= " (roomID,BookingID)";
						queryStr+= " VALUES ( ";
						queryStr+= "'"+roomhasBooking.getRoomId()+"',"+"'"+roomhasBooking.getBookingId()+"'\n";
						queryStr+= ")";
				
				recordCount=DBController.save(queryStr);
				}
			
			DBController.disconnect();
		} catch (Exception e) {
			System.out.println("Error at saveRoomHasBooking RoomHasBookingDaoImpl.");
			e.printStackTrace();
		}
		
		return recordCount;
	}
	public int deleteRoomHasBooking(int bookingId) {
		int recordCount=0;
		try {
			if(bookingId!=0) {
				String queryStr	 = " DELETE FROM Room_Has_Booking ";
						queryStr+= " WHERE BookingID="+bookingId;
						
				recordCount=DBController.save(queryStr);
				}
			
			DBController.disconnect();
		} catch (Exception e) {
			System.out.println("Error at saveBooking BookingDaoImpl.");
			e.printStackTrace();
		}
		
		return recordCount;
	}

}
